package com.performax.service;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.performax.database.Dao.*;

@Controller
@RequestMapping("/app/api/extra")
public class OheGameOuterController {
	private static final Logger logger = LoggerFactory.getLogger(OheGameOuterController.class);
	
	@Resource(name = "oh2gameGlobalDao")
	private Oh2GameGlobalDao oh2gameGlobalDao;
	
	@RequestMapping(value="/emailAuth")
	public String emailAuth(@RequestParam(value="akey", required=true) 		String akey
							,@RequestParam(value="uid", required=true) 		String userId
							,HttpServletRequest req )
	{
		if(akey	==	null	||	userId	==	null){
			return "redirect:"+"/app/api/extra/emailAuthFailed";
		}
		
		long	currentTime	=	System.currentTimeMillis();
		Date	now	=	new	Date(currentTime);
		HashMap<String, Object> param	=	new	HashMap<String, Object>();
		param.put("currentTime", now);
		param.put("authKey", akey);
		param.put("userId", userId);
		param.put("authType", "JOIN_EMAIL");
		
		HashMap<String, Object> authKeyInfo	=	oh2gameGlobalDao.getUserEmailAuthInfo(param);
		if(authKeyInfo	==	null	){
			return "redirect:/app/api/extra/emailAuthFailed";
		}
		
		try{
			if(currentTime	>	((Date)authKeyInfo.get("AUTH_REG_DT")).getTime()){
				return "redirect:"+"/app/api/extra/emailAuthTermExipred?uid="+userId;
			}
		}catch(NullPointerException e){
			e.printStackTrace();
			return "redirect:/app/api/extra/emailAuthFailed";
		}
		
		oh2gameGlobalDao.completeUserAuth(param);
		return "redirect:"+"/app/api/extra/emailAuthSuccess?uid="+userId;
	}
	
	@RequestMapping(value="/emailAuthSuccess")
	public String authSuccess(@RequestParam(value="uid", required=true) 		String userId
								,Model model
								,HttpServletRequest req )
	{
		if(userId	==	null	||	userId.equals("")){
			return "redirect:/app/api/extra/emailAuthFailed";
		}
		String marketUrl	=	oh2gameGlobalDao.getMarketUrl();
		model.addAttribute("userId", userId);
		model.addAttribute("marketUrl", marketUrl);
		return "success";
	}
	
	@RequestMapping(value="/emailAuthFailed")
	public String authFail(HttpServletRequest req ,Model model)
	{
		String marketUrl	=	oh2gameGlobalDao.getMarketUrl();
		
		model.addAttribute("marketUrl", marketUrl);
		return "failed";
	}
	
	@RequestMapping(value="/emailAuthTermExipred")
	public String termExpired(@RequestParam(value="uid", required=true) 		String userId
							,Model model
							,HttpServletRequest req )						
	{	
		if(userId	==	null	||	userId.equals("")){
			return "redirect:/app/api/extra/emailAuthFailed";
		}
		String marketUrl	=	oh2gameGlobalDao.getMarketUrl();
		
		model.addAttribute("marketUrl", marketUrl);
		model.addAttribute("userId", userId);
		return "termExpired";
	}
}

